package com.zhuzi.demo.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import com.zhuzi.demo.util.Utils;

public class BaseController {

    protected static enum ViewEnu {
        JSONVIEW("JSON", new MappingJackson2JsonView());

        private ViewEnu(String viewName, View view) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ((MappingJackson2JsonView) view).getObjectMapper().setDateFormat(sdf);
            this.viewName = viewName;
            this.view = view;
        }
        private String viewName;
        private View view;
        public String getViewName() {
            return viewName;
        }
        public void setViewName(String viewName) {
            this.viewName = viewName;
        }
        public View getView() {
            return view;
        }
        public void setView(View view) {
            this.view = view;
        }
    }

    public ModelAndView returnMav(Map<String, Object> params) {
        if (params == null) {
            params = new HashMap<String, Object>();
        }
        if (Utils.checkNull(params.get("code"))) {
            params.put("code", "0");
        }
        if (Utils.checkNull(params.get("msg"))) {
            params.put("msg", "成功!");
        }
        return new ModelAndView(ViewEnu.JSONVIEW.getView(), params);
    }
}